<template>
  <div v-if="true">
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
  <el-form-item label="账号" prop="userid">
    <el-input type="text" v-model="ruleForm.userid" autocomplete="off"></el-input>
  </el-form-item>
  <el-form-item label="密码" prop="userpassword">
    <el-input type="password" v-model="ruleForm.userpassword" autocomplete="off"></el-input>
  </el-form-item>
  <el-form-item>
    <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
    <el-button @click="resetForm('ruleForm')">重置</el-button>
  </el-form-item>
</el-form>
  </div>
  <div v-else>正在获取位置</div>
</template>

<script>
import axios from 'axios';
export default {
    name: 'LoginTest',
    data() {
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入账号'));
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        }
      };
      return {
        ruleForm: {
          userid: '',
          userpassword: '',
        },
        rules: {
          userid: [
            { validator: validatePass, trigger: 'blur' }
          ],
          userpassword: [
            { validator: validatePass2, trigger: 'blur' }
          ],
        },
        isLoading:true,
      };
    },
    methods: {   
      submitForm() {
         axios({
      url:"api/login/test",
      params:{
        id:this.ruleForm.userid,
        password:this.ruleForm.userpassword,
      }
    }).then(response => {
      if(response.data.data.type===0){
        if(response.data.data.token!==null){
          localStorage.setItem('token',response.data.data.token);
          localStorage.setItem('userid',this.ruleForm.userid);
        }
        this.$router.replace({
        name:'user',
        params:{userid: this.ruleForm.userid}
        })
      }else if(response.data.data.type===1){
        if(response.data.data.token!==null){
          localStorage.setItem('token',response.data.data.token);
        }
        this.$router.push({'path':'/admin'})
      }else if(response.data.data.type===-1){
        alert('用户名不存在或密码错误')
      }
    }).catch(error => {
      if(error.response.data===-3){
        alert('密钥失效，请联系管理员')
      }else if(error.response.data===-4){
        alert('禁止一台设备登录两个账号')
      }else if(error.response.data===-1){
        alert('用户名不存在或密码错误')
      }else if(error.response.data===-2){
        alert('暂不支持此浏览器')
      }else if(error.response.data===-5){
        alert("请使用第一次使登录的浏览器")
      }else if(error.response.data===-6){
        alert("请使用电脑访问")
      }
    });
      },

    },
  }
</script>

<style>

</style>